<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>webgl着色器</title>
  <script src="lib/webgl-debug.js"></script>
  <script src="lib/webgl-utils.js"></script>
  <script src="lib/cuon-utils.js"></script>
  <script src="lib/cuon-matrix.js"></script>
</head>

<body onload="main()">
  <canvas id="canvas" width="800" height="800"></canvas>
</body>
<script>
  // 顶点着色器伪代码
  var vShder_source = `
  attribute vec4 position;
  attribute vec4 color;
  uniform mat4 mat;
  varying vec4 col ;
void main(){
gl_Position = mat*position;
col=color;
}
`
  //片元着色器伪代码
  var fShder_source = `
  precision mediump float;
  varying vec4 col;
void main(){
gl_FragColor = col1;
}
`
  function main() {
    var canvas = document.getElementById('canvas');
    //  获取上下文对象
    var gl = getWebGLContext(canvas);
    //  初始化着色器
    if (!initShaders(gl, vShder_source, fShder_source)) {
      return;
    }

    var n = initVertexBuffers(gl);


    // 指定一个覆盖canvas的颜色
    gl.clearColor(0, 0, 0, 1.0);
    var mat = gl.getUniformLocation(gl.program, "mat");
    draw(gl, n,mat);

  }

  function draw(gl, n, mat) {
    //设置视觉矩阵的相关信息（视点，视线，上方向
    var vm = new Matrix4();
    vm.setLookAt(0, 0, 5, 0, 0,-100,  0, 1, 0);

    //设置模型矩阵相关信息；
    var mm = new Matrix4();
    mm.setRotate(0, 0, 0, 1);

    //设置透视投影矩阵
    var pm = new Matrix4();
    pm.setPerspective(30, canvas.width / canvas.height, 1, 10);

    // var orthMarix = new Matrix4();
    // orthMarix.setOrtho(-1, 1, -1, 1, 0, 1);

    //计算出模型视图矩阵
    var vmm = pm.multiply(vm.multiply(mm));
    gl.uniformMatrix4fv(mat, false, vmm.elements);
    gl.clear(gl.COLOR_BUFFER_BIT);
    gl.drawArrays(gl.TRIANGLES, 0, n);

  }

  function initVertexBuffers(gl) {

    var vertexDatas = new Float32Array([

      // 右侧的三个三角形
      0.75, 1.0, -4.0, 0.4, 1.0, 0.4, // 后面的绿色
      0.25, -1.0, -4.0, 0.4, 1.0, 0.4,
      1.25, -1.0, -4.0, 1.0, 0.4, 0.4,

      0.75, 1.0, -2.0, 1.0, 1.0, 0.4, // 中间的黄色
      0.25, -1.0, -2.0, 1.0, 1.0, 0.4,
      1.25, -1.0, -2.0, 1.0, 0.4, 0.4,

      0.75, 1.0, 0.0, 0.4, 0.4, 1.0,  // 前面的蓝色
      0.25, -1.0, 0.0, 0.4, 0.4, 1.0,
      1.25, -1.0, 0.0, 1.0, 0.4, 0.4,

      // 左侧的三个三角形
      -0.75, 1.0, -4.0, 0.4, 1.0, 0.4, // 后面的绿色
      -1.25, -1.0, -4.0, 0.4, 1.0, 0.4,
      -0.25, -1.0, -4.0, 1.0, 0.4, 0.4,

      -0.75, 1.0, -2.0, 1.0, 1.0, 0.4, // 中间的黄色
      -1.25, -1.0, -2.0, 1.0, 1.0, 0.4,
      -0.25, -1.0, -2.0, 1.0, 0.4, 0.4,

      -0.75, 1.0, 0.0, 0.4, 0.4, 1.0,  // 前面的蓝色
      -1.25, -1.0, 0.0, 0.4, 0.4, 1.0,
      -0.25, -1.0, 0.0, 1.0, 0.4, 0.4


    ]);

    //创建缓冲区
    var vertexbuffer = gl.createBuffer();
    //绑定缓冲区
    gl.bindBuffer(gl.ARRAY_BUFFER, vertexbuffer);
    //向缓冲区写入数据
    gl.bufferData(gl.ARRAY_BUFFER, vertexDatas, gl.STATIC_DRAW);
    var size = vertexDatas.BYTES_PER_ELEMENT;
    var position = gl.getAttribLocation(gl.program, "position");
    var color = gl.getAttribLocation(gl.program, "color");

    gl.vertexAttribPointer(position, 3, gl.FLOAT, false, size * 6, 0);
    gl.enableVertexAttribArray(position);

    gl.vertexAttribPointer(color, 3, gl.FLOAT, false, size * 6, size * 3);
    gl.enableVertexAttribArray(color);

    return 18;
  }

</script>

</html>